package com.lyx.question.controller;

import com.lyx.common.result.R;
import com.lyx.common.utils.PageUtils;
import com.lyx.question.dto.ESQuestionDto;
import com.lyx.question.dto.SearchResult;
import com.lyx.question.entity.ESQuestion;
import com.lyx.question.service.QuestionService;
import com.lyx.question.vo.QuestionVo;
import com.lyx.question.vo.SearchParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 题库
 * @author 14667
 * @date 2022/7/20 15:25
 */
@RestController
@RequestMapping("/question")
@CrossOrigin
public class QuestionController {
    @Autowired
    private QuestionService questionService;

    /**
     * 添加题目
     * @param questionVo
     * @return
     */
    @PostMapping("/addQuestion")
    public R addQuestion(@Validated @RequestBody QuestionVo questionVo){
        boolean b = questionService.addQuestion(questionVo);
        if(!b){
            return R.ok().message("添加题目失败");
        }
        return R.ok().message("添加题目成功");
    }

    /**
     * 更新题目
     * @param questionVo
     * @return
     */

    @PostMapping("/updateQuestion")
    public R updateQuestion(@Validated @RequestBody QuestionVo questionVo){
        boolean b = questionService.updateQuestion(questionVo);
        if(!b){
            return R.error().message("修改题目失败");
        }else{
            return R.ok().message("修改题目成功");
        }
    }
    /**
     * 删除题目
     * @param id
     * @return
     */
    @PutMapping("/deleteQuestion")
    public R deleteQuestion(@RequestParam Integer id){
        boolean b = questionService.deleteQuestion(id);
        if(b){
            return R.ok().message("删除题目成功");
        }
        return R.error().message("删除题目失败");
    }

    /**
     * 根据题号和章节id分页查询
     * @param id
     * @param chapterId
     * @param currentPage
     * @param size
     * @return
     */
    @GetMapping("/selectQuestionByIdAndChapterId")
    public R selectQuestionByIdAndChapterId(@RequestParam(required = false) Integer id,@RequestParam(required = false) Integer chapterId,
                                            @RequestParam(defaultValue = "1",required = false) Integer currentPage,@RequestParam(defaultValue = "10",required = false) Integer size){
        PageUtils questionList =  questionService.selectQuestionByIdAndChapterId(id,chapterId,currentPage,size);
        return R.ok().message("数据查询成功").data("data",questionList);
    }

    /**
     * 分页查询题目
     * @param currentPage
     * @param size
     * @return
     */
    @GetMapping("selectAll")
    public R selectAll(@RequestParam(defaultValue = "1",required = false) Integer currentPage,@RequestParam(defaultValue = "10",required = false) Integer size){
        PageUtils list = questionService.listWithPage(currentPage,size);
        return R.ok().message("数据查询成功").data("data",list);
    }


//    /**
//     * 将2000道题保存到数据库
//     */
//    @PutMapping("saveAll")
//    public R saveAll(){
//        questionService.saveAll();
//        return R.ok();
//    }

    /**
     * es首页搜索
     * @param text
     * @param currentPage
     * @param size
     * @return
     */
    @GetMapping("/indexSearch")
    public R indexSearch(@RequestParam String text,@RequestParam Long currentPage,@RequestParam Long size){
        SearchResult data = questionService.indexSearch(text,currentPage,size);

        return R.ok().data("data",data).message("搜索完成");
    }

    /**
     * 手动更新排名
     * @return
     */
    @GetMapping("shcduleUpdateRank")
    public R schduleUpdateRank(){
        questionService.updateRank();
        return R.ok().message("更新排名成功");
    }
}
